package com.icinfo.cloud.provider.common.utils;

import cn.hutool.core.util.ObjectUtil;
import org.springframework.util.ObjectUtils;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.lang.reflect.Field;

/**
 * 描述：自定义参数校验
 * @author shishengyao
 * @description
 * @date 2022/5/11
 */
public class CustomCheckParamUtil {

    public static <T> void doCheck(T t) {
        final Field[] declaredFields = t.getClass().getDeclaredFields();
        for (Field declaredField : declaredFields) {
            // 当前变量是这个 Check 接口的实现类、或有NotNull注解
            if (declaredField.isAnnotationPresent(NotNull.class)
                    || declaredField.isAnnotationPresent(NotEmpty.class)) {
                declaredField.setAccessible(true);
                deepCheck(declaredField, t);
            }
        }
    }

    private static <T> void deepCheck(Field declaredField, T t) {
        try {
            final Object declaredFieldValue = declaredField.get(t);
            if (declaredFieldValue == null && declaredField.isAnnotationPresent(NotNull.class)) {
                final NotNull notNull = declaredField.getAnnotation(NotNull.class);
                final String errorMsg = notNull.message();
                throw new IllegalArgumentException(errorMsg);
            }
            if (ObjectUtil.isEmpty(declaredFieldValue) && declaredField.isAnnotationPresent(NotEmpty.class)) {
                final NotEmpty notEmpty = declaredField.getAnnotation(NotEmpty.class);
                final String errorMsg = notEmpty.message();
                throw new IllegalArgumentException(errorMsg);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    /**
     * 校验参数是否全部不为空
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> boolean doCheckAllParamIsNotEmpty(T t) {
        boolean flag = true;
        try {
            final Field[] declaredFields = t.getClass().getDeclaredFields();
            for (Field declaredField : declaredFields) {
                declaredField.setAccessible(true);
                if (ObjectUtils.isEmpty(declaredField.get(t))) {
                    flag = false;
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    /**
     * 检验参数是否全部为空
     *
     * @param obj
     * @return
     * @throws Exception
     */
    public static boolean isFieldsNull(Object obj) {
        try {
            if (obj == null) {
                return true;
            }
            for (Field field : obj.getClass().getDeclaredFields()) { // 获取实体对象的所有属性
                field.setAccessible(true);
                if (field.get(obj) != null) { // 判断实体对象的属性是否为空
                    return false;
                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }
}
